package com.ZYHsheep.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtil {
    private static final Properties PROP = new Properties();

    static {
        try (InputStream in = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties")) {
            PROP.load(in);
            Class.forName(PROP.getProperty("driver"));
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException("加载数据库配置失败", e);
        }
    }

    public static Connection getConn() throws SQLException {
        return DriverManager.getConnection(
                PROP.getProperty("url"),
                PROP.getProperty("user"),
                PROP.getProperty("password"));
    }

    public static void close(AutoCloseable... acs) {
        for (AutoCloseable ac : acs) {
            if (ac != null) try { ac.close(); } catch (Exception ignored) {}
        }
    }
}